﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data.SqlClient;
using System.ServiceProcess;
using www.serviciipeweb.ro.CDC;
using System.Diagnostics;

namespace www.serviciipeweb.ro.CDC
{
    public enum eServerCDC
    {        
        OK = 0,
        WrongVersion = 1,
        Express =2,
        AgentNotRunning = 3
    }

    public static class ServerHelper
    {
        

        //public static void ConnectToServer(this Server obj,string ServerName, String UID, string PWD)            
        //{
                
        //    ServerConnection sc = new ServerConnection
        //        {LoginSecure = false,ServerInstance = ServerName};
        //    sc.Login = UID;
        //    sc.Password = PWD;
        //    sc.Connect();

        //    obj = new Server(sc);
        //}
        //public static void ConnectToServer(this Server obj,string ServerName)            
        //{
        //    ServerConnection sc = new ServerConnection();
        //    sc.LoginSecure = true;
        //    sc.ServerInstance = ServerName ;            
        //    sc.Connect();
            
        //    obj = new Server(sc);
            
        //}
        public static Database FromID(this Server obj, int ID)
        {
            return obj.Databases.Cast<Database>().SingleOrDefault<Database>
                (
                (d) => { return d.ID == ID; }
                );
        }
        public static List<Database> NonSystemDatabases(this Server obj)
        {
            List<Database> l = new List<Database>();
            foreach (Database d in obj.Databases)
            {
                if(!d.IsSystemObject)
                    l.Add(d);

            }
            return l;
        }
        public static bool IsCDCEnabled(this Server obj)
        {
            eServerCDC e;
            return obj.IsCDCEnabled(out e);
        }
        public static bool IsCDCEnabled(this Server obj, out eServerCDC cause)
        {

            cause = eServerCDC.OK;

            if(obj.Version.Major < 10)
            {                
                cause = eServerCDC.WrongVersion;
                return false;
            }
            if (obj.Edition.ToString() == Edition.Express.ToString())
            {
                cause = eServerCDC.Express;
                return false;
            }
            if (!obj.JobServer.IsRunning())
            {
                cause = eServerCDC.AgentNotRunning;
                return false;
            }
            
            return (cause == eServerCDC.OK);
        }
        public static bool EnableCDC(this Server s)
        {
            eServerCDC cause;
            if (s.IsCDCEnabled(out cause))
                return true;

            if (cause != eServerCDC.AgentNotRunning)
                return false;

            return s.JobServer.StartAgent();

        }
        
        
    }
}
